package com.quanyan.messagepush.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.RequestContextFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.Filter;
import javax.servlet.ServletContext;

@Configuration
@ComponentScan(basePackages = {
        "com.quanyan.redisCluster.config",
        "com.quanyan.common.exception.handler", //全局异常捕获处理
        "com.quanyan.common.interceptor",     //参数校验拦截器, RequestBody, ResponseBody拦截. controllerAround拦截
        "com.quanyan.*.component",
        "com.quanyan.*.biz",
        "com.quanyan.*.service",
        "com.quanyan.*.web",
        "com.quanyan.*.cache",
        "com.quanyan.*.wrapper"
})
@EnableAutoConfiguration
//@MapperScan(basePackages = {"com.quanyan.*.mapper"})
@ImportResource({"classpath:spring/application-profile.xml"})
@PropertySource("classpath:application.properties")
@EnableScheduling
public class Application extends WebMvcConfigurerAdapter {

    /**
     * 启动器
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.run(args);
    }

//    @Bean(name = "dataSource", destroyMethod = "close", initMethod = "init")
//    @ConfigurationProperties(prefix = "spring.datasource", locations = "classpath:application.properties")
//    public DataSource dataSource() {
//        DruidDataSource druidDataSource = new DruidDataSource();
//        return druidDataSource;
//    }
//
//
//    @Bean
//    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        sqlSessionFactoryBean.setDataSource(dataSource());
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/com/quanyan/*/mapper/*.xml"));
//        return sqlSessionFactoryBean.getObject();
//    }

//
//    @Bean
//    public PlatformTransactionManager transactionManager(DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }

    @Bean
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }


    @Bean
    public RequestContextFilter requestContextFilter(ServletContext servletContext) {
        RequestContextFilter requestContextFilter =new RequestContextFilter();
        requestContextFilter.setServletContext(servletContext);
        requestContextFilter.setThreadContextInheritable(false);
        return requestContextFilter;
    }



}
